﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
   public class MenuMemberDAO//:BaseDAO<RELAXEntities,RelaxMenu_ManagerMember>
   {
       #region value
       private readonly RelaxDataContext _context = new RelaxDataContext();
       #endregion

       #region hàm lấy dữ liệu

       public IQueryable<RelaxMenuMemberDataContract> GetModulDataContracts()
       {
           var query = (from v in _context.RelaxMenu_ManagerMembers 
                       join b in _context.RelaxLogins on v.managermemberId equals b.id
               select new RelaxMenuMemberDataContract
               {
                   Id = Convert.ToInt32(v.id),
                   Active= Convert.ToInt32(v.active),
                   MemberId = Convert.ToInt32(v.managermemberId),
                   MenuId = Convert.ToInt32(v.menuId),
                   MemberName = b.username
               }).Distinct();
         
           return query;
       }
       #endregion

       #region hàm Add

       
       #endregion

       #region Add
       public void Add(RelaxMenu_ManagerMember menuManagerMember)
       {
           _context.RelaxMenu_ManagerMembers.InsertOnSubmit(menuManagerMember);
           _context.SubmitChanges();

       }
       #endregion

       #region hàm Update

       public void Update(RelaxMenu_ManagerMember menuManagerMember)
       {
           var obj = _context.RelaxMenu_ManagerMembers.Where(m => m.id == menuManagerMember.id);
           if (obj.Any())
           {
               var firstOrdefault = obj.FirstOrDefault();
               if (firstOrdefault != null)
               {
                   firstOrdefault.active = menuManagerMember.active;
                   firstOrdefault.menuId = menuManagerMember.menuId;
                   firstOrdefault.managermemberId = menuManagerMember.managermemberId;

                   _context.SubmitChanges();
               }
           }
       }
       #endregion

       #region hàm Delete

       public void Delete(long iId)
       {
           var obj = _context.RelaxMenu_ManagerMembers.Where(m => m.id == iId);
           if (obj.Any())
           {
                // ReSharper disable once AssignNullToNotNullAttribute
               _context.RelaxMenu_ManagerMembers.DeleteOnSubmit(obj.FirstOrDefault());
               _context.SubmitChanges();
           }
       }
       #endregion

   }
}
